using Boo.Lang.Runtime;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityScript.Lang;

[Serializable]
public class cityGenerator : MonoBehaviour
{
	public GameObject tileObj;

	public float tileSize;

	public float cityWidth;

	public float cityLength;

	public float roadDensity;

	public bool debugProgressToConsole;

	[Header("Road Tiles")]
	public GameObject fourWayRoad;

	public GameObject tRoad;

	public GameObject rightAngleRoad;

	public GameObject straightRoad;

	public GameObject endRoad;

	[Header("Building Objects")]
	public GameObject emptySpace;

	public GameObject[] buildings;

	private GameObject fence;

	[Header("UI Fields (Ignore if generating at runtime)")]
	public Text customCityWidth;

	public Text customCityLength;

	public Text customCityBranching;

	private int xWidth;

	private int zLength;

	private List<GameObject> allCityTiles;

	private List<GameObject> allRoadTiles;

	private List<GameObject> allBuildingTiles;

	private List<GameObject> allRoadObjs;

	private List<GameObject> allBuildingObjs;

	private GameObject cityParent;

	public cityGenerator()
	{
		allCityTiles = new List<GameObject>();
		allRoadTiles = new List<GameObject>();
		allBuildingTiles = new List<GameObject>();
		allRoadObjs = new List<GameObject>();
		allBuildingObjs = new List<GameObject>();
	}

	public void Start()
	{
		buildCity();
	}

	public void buildCity()
	{
		deleteCity();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Previous city deleted");
		}
		clearCache();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Previous city cache cleared");
		}
		makeGrid();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("New city grid created");
		}
		createRoadNetwork(allCityTiles[UnityEngine.Random.Range(0, allCityTiles.Count)], getRandomRoadDirection(), xWidth);
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Road network created");
		}
		alignRoads();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Road objects placed and oriented");
		}
		makeBuildings();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("City buildings created");
		}
		fillEmptySpace();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Empty space filled");
		}
		destroyAllTiles();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("Grid tiles cleaned up");
		}
		parentAllObjects();
		if (debugProgressToConsole)
		{
			UnityEngine.Debug.Log("All objects parented to city gameobject");
		}
	}

	public void fenceOffSpace()
	{
		RaycastHit hitInfo = default(RaycastHit);
		GameObject gameObject = null;
		Vector3 vector = default(Vector3);
		IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(allBuildingTiles);
		while (enumerator.MoveNext())
		{
			object obj = enumerator.Current;
			if (!(obj is GameObject))
			{
				obj = RuntimeServices.Coerce(obj, typeof(GameObject));
			}
			GameObject gameObject2 = (GameObject)obj;
			if (Physics.Raycast(gameObject2.transform.position, Vector3.forward, out hitInfo, tileSize) && hitInfo.collider.tag == "freeTile")
			{
				vector = gameObject2.transform.position + 0.5f * (hitInfo.collider.transform.position - gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				gameObject = (GameObject)UnityEngine.Object.Instantiate(fence, vector, fence.transform.rotation);
				gameObject.transform.LookAt(gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				allBuildingObjs.Add(gameObject);
			}
			if (Physics.Raycast(gameObject2.transform.position, -Vector3.forward, out hitInfo, tileSize) && hitInfo.collider.tag == "freeTile")
			{
				vector = gameObject2.transform.position + 0.5f * (hitInfo.collider.transform.position - gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				gameObject = (GameObject)UnityEngine.Object.Instantiate(fence, vector, fence.transform.rotation);
				gameObject.transform.LookAt(gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				allBuildingObjs.Add(gameObject);
			}
			if (Physics.Raycast(gameObject2.transform.position, Vector3.right, out hitInfo, tileSize) && hitInfo.collider.tag == "freeTile")
			{
				vector = gameObject2.transform.position + 0.5f * (hitInfo.collider.transform.position - gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				gameObject = (GameObject)UnityEngine.Object.Instantiate(fence, vector, fence.transform.rotation);
				gameObject.transform.LookAt(gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				allBuildingObjs.Add(gameObject);
			}
			if (Physics.Raycast(gameObject2.transform.position, -Vector3.right, out hitInfo, tileSize) && hitInfo.collider.tag == "freeTile")
			{
				vector = gameObject2.transform.position + 0.5f * (hitInfo.collider.transform.position - gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				gameObject = (GameObject)UnityEngine.Object.Instantiate(fence, vector, fence.transform.rotation);
				gameObject.transform.LookAt(gameObject2.transform.position);
				UnityRuntimeServices.Update(enumerator, gameObject2);
				allBuildingObjs.Add(gameObject);
			}
		}
	}

	public void fillEmptySpace()
	{
		IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(allCityTiles);
		while (enumerator.MoveNext())
		{
			object obj = enumerator.Current;
			if (!(obj is GameObject))
			{
				obj = RuntimeServices.Coerce(obj, typeof(GameObject));
			}
			GameObject gameObject = (GameObject)obj;
			if (gameObject.tag == "freeTile")
			{
				GameObject gameObject2 = (GameObject)UnityEngine.Object.Instantiate(emptySpace, gameObject.transform.position, emptySpace.transform.rotation);
				UnityRuntimeServices.Update(enumerator, gameObject);
				allBuildingObjs.Add(gameObject2);
				gameObject2.SetActive(value: false);
			}
		}
	}

	public void destroyAllTiles()
	{
		IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(allCityTiles);
		while (enumerator.MoveNext())
		{
			object obj = enumerator.Current;
			if (!(obj is GameObject))
			{
				obj = RuntimeServices.Coerce(obj, typeof(GameObject));
			}
			GameObject gameObject = (GameObject)obj;
			UnityEngine.Object.DestroyImmediate(gameObject);
			UnityRuntimeServices.Update(enumerator, gameObject);
		}
	}

	public void parentAllObjects()
	{
		GameObject gameObject = new GameObject();
		gameObject.name = "city";
		allBuildingObjs.Add(gameObject);
		cityParent = gameObject;
		IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(allRoadObjs);
		while (enumerator.MoveNext())
		{
			object obj = enumerator.Current;
			if (!(obj is GameObject))
			{
				obj = RuntimeServices.Coerce(obj, typeof(GameObject));
			}
			GameObject gameObject2 = (GameObject)obj;
			gameObject2.transform.parent = gameObject.transform;
			UnityRuntimeServices.Update(enumerator, gameObject2);
			gameObject2.SetActive(value: true);
			UnityRuntimeServices.Update(enumerator, gameObject2);
		}
		IEnumerator enumerator2 = UnityRuntimeServices.GetEnumerator(allBuildingObjs);
		while (enumerator2.MoveNext())
		{
			object obj2 = enumerator2.Current;
			if (!(obj2 is GameObject))
			{
				obj2 = RuntimeServices.Coerce(obj2, typeof(GameObject));
			}
			GameObject gameObject3 = (GameObject)obj2;
			gameObject3.transform.parent = gameObject.transform;
			UnityRuntimeServices.Update(enumerator2, gameObject3);
			gameObject3.SetActive(value: true);
			UnityRuntimeServices.Update(enumerator2, gameObject3);
		}
	}

	public void deleteCity()
	{
		if ((bool)cityParent)
		{
			UnityEngine.Object.DestroyImmediate(cityParent);
		}
	}

	public void clearCache()
	{
		cityParent = null;
		allCityTiles.Clear();
		allRoadTiles.Clear();
		allBuildingTiles.Clear();
		allRoadObjs.Clear();
		allBuildingObjs.Clear();
	}

	public void makeBuildings()
	{
		GameObject[] array = GameObject.FindGameObjectsWithTag("freeTile");
		int i = 0;
		GameObject[] array2 = array;
		for (int length = array2.Length; i < length; i++)
		{
			GameObject gameObject = null;
			RaycastHit hitInfo = default(RaycastHit);
			if (Physics.Raycast(array2[i].transform.position, Vector3.forward, out hitInfo, tileSize) && hitInfo.collider.tag == "roadTile")
			{
				gameObject = hitInfo.collider.gameObject;
			}
			if (Physics.Raycast(array2[i].transform.position, -Vector3.forward, out hitInfo, tileSize) && hitInfo.collider.tag == "roadTile")
			{
				gameObject = hitInfo.collider.gameObject;
			}
			if (Physics.Raycast(array2[i].transform.position, Vector3.right, out hitInfo, tileSize) && hitInfo.collider.tag == "roadTile")
			{
				gameObject = hitInfo.collider.gameObject;
			}
			if (Physics.Raycast(array2[i].transform.position, -Vector3.right, out hitInfo, tileSize) && hitInfo.collider.tag == "roadTile")
			{
				gameObject = hitInfo.collider.gameObject;
			}
			if ((bool)gameObject)
			{
				array2[i].transform.LookAt(gameObject.transform.position);
				array2[i].tag = "buildingTile";
				GameObject[] array3 = buildings;
				float num = Vector3.Distance(array2[i].transform.position, new Vector3(cityWidth / 2f, 0f, cityLength / 2f));
				GameObject original = array3[UnityEngine.Random.Range(0, array3.Length)];
				GameObject gameObject2 = (GameObject)UnityEngine.Object.Instantiate(original, array2[i].transform.position, array2[i].transform.rotation);
				allBuildingObjs.Add(gameObject2);
				allBuildingTiles.Add(array2[i]);
				gameObject2.SetActive(value: false);
			}
		}
	}

	public void makeGrid()
	{
		xWidth = Mathf.RoundToInt(cityWidth / tileSize);
		zLength = Mathf.RoundToInt(cityLength / tileSize);
		Vector3 zero = Vector3.zero;
		Quaternion rotation = tileObj.transform.rotation;
		tileObj.transform.localScale = new Vector3(1f, 1f, 1f) * tileSize;
		for (int i = 0; i < zLength; i++)
		{
			zero.x = 0f;
			zero.z += tileSize;
			for (int j = 0; j < xWidth; j++)
			{
				GameObject gameObject = (GameObject)UnityEngine.Object.Instantiate(tileObj, zero, rotation);
				zero.x += tileSize;
				gameObject.SetActive(value: true);
				gameObject.tag = "freeTile";
				allCityTiles.Add(gameObject);
			}
		}
	}

	public void createRoadNetwork(GameObject startTile, Vector3 direction, int life)
	{
		GameObject gameObject = startTile;
		bool flag = true;
		for (int i = 0; i < life; i++)
		{
			gameObject.tag = "roadTile";
			Vector3 position = gameObject.transform.position;
			allRoadTiles.Add(gameObject);
			RaycastHit hitInfo = default(RaycastHit);
			bool flag2 = Physics.Raycast(position, direction, out hitInfo, tileSize);
			if (flag2)
			{
				if (hitInfo.collider.tag == "freeTile")
				{
					gameObject = hitInfo.collider.gameObject;
				}
				if (hitInfo.collider.tag == "roadTile")
				{
					break;
				}
			}
			if (!flag2)
			{
				break;
			}
			float value = UnityEngine.Random.value;
			if (!(value >= roadDensity) && !flag)
			{
				flag = true;
				Vector3 direction2 = Vector3.Cross(direction, Vector3.up) * Mathf.Sign(UnityEngine.Random.Range(-1, 1));
				RaycastHit hitInfo2 = default(RaycastHit);
				if (Physics.Raycast(position, direction2, out hitInfo2, tileSize) && hitInfo2.collider.tag == "freeTile")
				{
					createRoadNetwork(hitInfo2.collider.gameObject, direction2, xWidth);
				}
			}
			if (!(value < roadDensity))
			{
				flag = false;
			}
		}
	}

	public Vector3 getRandomRoadDirection()
	{
		Vector3 result = Vector3.forward;
		float value = UnityEngine.Random.value;
		if (!(value > 0.33f))
		{
			result = -Vector3.forward;
		}
		if (!(value > 0.66f) && !(value <= 0.33f))
		{
			result = -Vector3.right;
		}
		if (!(value <= 0.66f))
		{
			result = Vector3.right;
		}
		return result;
	}

	public void alignRoads()
	{
		IEnumerator enumerator = UnityRuntimeServices.GetEnumerator(allRoadTiles);
		while (enumerator.MoveNext())
		{
			object obj = enumerator.Current;
			if (!(obj is GameObject))
			{
				obj = RuntimeServices.Coerce(obj, typeof(GameObject));
			}
			GameObject gameObject = (GameObject)obj;
			alignRoad(gameObject);
			UnityRuntimeServices.Update(enumerator, gameObject);
		}
	}

	public void alignRoad(GameObject roadSegment)
	{
		RaycastHit hitInfo = default(RaycastHit);
		GameObject gameObject = null;
		bool flag = false;
		bool flag2 = false;
		bool flag3 = false;
		bool flag4 = false;
		bool flag5 = false;
		if (Physics.Raycast(roadSegment.transform.position, Vector3.forward, out hitInfo) && hitInfo.collider.tag == "roadTile")
		{
			flag = true;
		}
		if (Physics.Raycast(roadSegment.transform.position, -Vector3.forward, out hitInfo) && hitInfo.collider.tag == "roadTile")
		{
			flag2 = true;
		}
		if (Physics.Raycast(roadSegment.transform.position, Vector3.right, out hitInfo) && hitInfo.collider.tag == "roadTile")
		{
			flag4 = true;
		}
		if (Physics.Raycast(roadSegment.transform.position, -Vector3.right, out hitInfo) && hitInfo.collider.tag == "roadTile")
		{
			flag3 = true;
		}
		if (!flag5 && flag && flag2 && flag3 && flag4)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(fourWayRoad, roadSegment.transform.position, fourWayRoad.transform.rotation);
			flag5 = true;
		}
		if (!flag5 && flag && flag4 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(tRoad, roadSegment.transform.position, tRoad.transform.rotation);
			Vector3 eulerAngles = gameObject.transform.eulerAngles;
			float y = eulerAngles.y + 90f;
			Vector3 eulerAngles2 = gameObject.transform.eulerAngles;
			eulerAngles2.y = y;
			Vector3 vector2 = gameObject.transform.eulerAngles = eulerAngles2;
			flag5 = true;
		}
		if (!flag5 && flag && flag2 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(tRoad, roadSegment.transform.position, tRoad.transform.rotation);
			flag5 = true;
		}
		if (!flag5 && flag2 && flag4 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(tRoad, roadSegment.transform.position, tRoad.transform.rotation);
			Vector3 eulerAngles3 = gameObject.transform.eulerAngles;
			float y2 = eulerAngles3.y - 90f;
			Vector3 eulerAngles4 = gameObject.transform.eulerAngles;
			eulerAngles4.y = y2;
			Vector3 vector4 = gameObject.transform.eulerAngles = eulerAngles4;
			flag5 = true;
		}
		if (!flag5 && flag && flag2 && flag4)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(tRoad, roadSegment.transform.position, tRoad.transform.rotation);
			Vector3 eulerAngles5 = gameObject.transform.eulerAngles;
			float y3 = eulerAngles5.y + 180f;
			Vector3 eulerAngles6 = gameObject.transform.eulerAngles;
			eulerAngles6.y = y3;
			Vector3 vector6 = gameObject.transform.eulerAngles = eulerAngles6;
			flag5 = true;
		}
		if (!flag5 && flag && flag4)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(rightAngleRoad, roadSegment.transform.position, rightAngleRoad.transform.rotation);
			Vector3 eulerAngles7 = gameObject.transform.eulerAngles;
			float y4 = eulerAngles7.y + 90f;
			Vector3 eulerAngles8 = gameObject.transform.eulerAngles;
			eulerAngles8.y = y4;
			Vector3 vector8 = gameObject.transform.eulerAngles = eulerAngles8;
			flag5 = true;
		}
		if (!flag5 && flag && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(rightAngleRoad, roadSegment.transform.position, rightAngleRoad.transform.rotation);
			flag5 = true;
		}
		if (!flag5 && flag2 && flag4)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(rightAngleRoad, roadSegment.transform.position, rightAngleRoad.transform.rotation);
			Vector3 eulerAngles9 = gameObject.transform.eulerAngles;
			float y5 = eulerAngles9.y + 180f;
			Vector3 eulerAngles10 = gameObject.transform.eulerAngles;
			eulerAngles10.y = y5;
			Vector3 vector10 = gameObject.transform.eulerAngles = eulerAngles10;
			flag5 = true;
		}
		if (!flag5 && flag2 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(rightAngleRoad, roadSegment.transform.position, rightAngleRoad.transform.rotation);
			Vector3 eulerAngles11 = gameObject.transform.eulerAngles;
			float y6 = eulerAngles11.y - 90f;
			Vector3 eulerAngles12 = gameObject.transform.eulerAngles;
			eulerAngles12.y = y6;
			Vector3 vector12 = gameObject.transform.eulerAngles = eulerAngles12;
			flag5 = true;
		}
		if (!flag5 && flag && flag2)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(straightRoad, roadSegment.transform.position, straightRoad.transform.rotation);
			flag5 = true;
		}
		if (!flag5 && flag4 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(straightRoad, roadSegment.transform.position, straightRoad.transform.rotation);
			Vector3 eulerAngles13 = gameObject.transform.eulerAngles;
			float y7 = eulerAngles13.y - 90f;
			Vector3 eulerAngles14 = gameObject.transform.eulerAngles;
			eulerAngles14.y = y7;
			Vector3 vector14 = gameObject.transform.eulerAngles = eulerAngles14;
			flag5 = true;
		}
		if (!flag5 && flag4)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(endRoad, roadSegment.transform.position, endRoad.transform.rotation);
			Vector3 eulerAngles15 = gameObject.transform.eulerAngles;
			float y8 = eulerAngles15.y + 90f;
			Vector3 eulerAngles16 = gameObject.transform.eulerAngles;
			eulerAngles16.y = y8;
			Vector3 vector16 = gameObject.transform.eulerAngles = eulerAngles16;
			flag5 = true;
		}
		if (!flag5 && flag3)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(endRoad, roadSegment.transform.position, endRoad.transform.rotation);
			Vector3 eulerAngles17 = gameObject.transform.eulerAngles;
			float y9 = eulerAngles17.y - 90f;
			Vector3 eulerAngles18 = gameObject.transform.eulerAngles;
			eulerAngles18.y = y9;
			Vector3 vector18 = gameObject.transform.eulerAngles = eulerAngles18;
			flag5 = true;
		}
		if (!flag5 && flag)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(endRoad, roadSegment.transform.position, endRoad.transform.rotation);
			flag5 = true;
		}
		if (!flag5 && flag2)
		{
			gameObject = (GameObject)UnityEngine.Object.Instantiate(endRoad, roadSegment.transform.position, endRoad.transform.rotation);
			Vector3 eulerAngles19 = gameObject.transform.eulerAngles;
			float y10 = eulerAngles19.y - 180f;
			Vector3 eulerAngles20 = gameObject.transform.eulerAngles;
			eulerAngles20.y = y10;
			Vector3 vector20 = gameObject.transform.eulerAngles = eulerAngles20;
			flag5 = true;
		}
		if ((bool)gameObject)
		{
			allRoadObjs.Add(gameObject);
			gameObject.SetActive(value: false);
		}
	}

	public void updateCityWidth()
	{
		if (!string.IsNullOrEmpty(customCityWidth.text))
		{
			cityWidth = float.Parse(customCityWidth.text);
		}
	}

	public void updateCityLength()
	{
		if (!string.IsNullOrEmpty(customCityLength.text))
		{
			cityLength = float.Parse(customCityLength.text);
		}
	}

	public void updateCityBranching()
	{
		if (!string.IsNullOrEmpty(customCityBranching.text))
		{
			roadDensity = float.Parse(customCityBranching.text);
		}
	}

	public void Main()
	{
	}
}
